#!/bin/sh
#
# Copyright (c) 2007-2009 The PureDarwin Project.
# All rights reserved.
#
# @LICENSE_HEADER_START@
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# @LICENSE_HEADER_END@
#

#
# aladin   <aladin@puredarwin.org>
#

#
# PureDarwin install (wrapper) script
#
#	Usage: pd_installer
#
# This script should not be run manually but by launchd via its respective plist.
#
# Based on the "OpenDarwin install script" by kvv
# $Id: rc.cdrom,v 1.30 2005/04/28 20:06:46 kevin Exp $
#
# Based on the "PureDarwin boot installer" (pd_installer_1) by Stuart Crook
#	v1.0 31/03/09 Initial release 

###############################################################################
# GLOBAL VARIABLES AND SETTINGS 
###############################################################################

# Redirecting stdin  (/dev/stdin  -> /dev/fd/0)
exec 0</dev/console
# Redirecting stdout (/dev/stdout -> /dev/fd/1)
exec 1>/dev/console
# Redirecting stderr (/dev/stderr -> /dev/fd/2)
exec 2>/dev/console

# File creation mask
umask 022			# all files created are -rw-r--r--

# Env
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/libexec:/System/Library/CoreServices
export MANPATH=/usr/share/man:/usr/local/man
export EDITOR=/usr/bin/vi
export TERM=xterm-color

# Setting hostname
sysctl -w kern.hostname="PureDarwin.local"

# Populating /dev/fd/ with mount_fdesc
/bin/df | /usr/bin/grep fdesc
if [ ! $? -eq 0 ]; then
	/sbin/mount -t fdesc -o union stdin /dev
fi

# Workaround read-write
#/sbin/mount -uw /

# Find out where this script lives
DIRNAME=$(cd $(dirname $0) && pwd)

###############################################################################
# FUNCTIONS 
###############################################################################

add_user () {

	TARGET="$1"
	MOUNT="/Volumes/$2"

	# The mount point must exist (prior) without ramdisk
	#mkdir -p "$MOUNT"
	# FIXME s1 is static.
	/sbin/mount -t hfs "${TARGET}s1" "$MOUNT"
	
	chroot $MOUNT /usr/local/share/puredarwin/scripts/pd_injectuser /
	# Alternative: FIXME add dscl / pd_adduser

	/sbin/umount -f $MOUNT
}

welcome() {

	clear

	#echo
	#echo "Welcome to PureDarwin!" # /etc/motd
	#echo
	#echo " Darwin (c) 1999-2009 Apple, Inc. (opensource.apple.com)"
	#echo " Purity (c) 2007-2009 The PureDarwin Project (puredarwin.org)"
	#echo

	# Above replaced by `pd_xkp' built on PureDarwin (D9, i386) displaying the same message as above.
	/usr/local/share/puredarwin/projects/pd_xkp/pd_xkp

	#if [ -f "/Release Notes.txt" ]; then
	#	echo ""
	#	echo "Welcome to the installation of" `grep 'Darwin [0-9.-]*' /Release\ Notes.txt | head -1 | awk '{print $1" "$2}'`
	#	echo ""
	#fi
	#if [ -f "/Important Notice.txt" ]; then
	#	cat "/Important Notice.txt"
	#fi
}

###############################################################################
# INTRO 
###############################################################################

welcome

echo
echo "Enter 's' to drop into a shell."
echo

###############################################################################
# DISK DEVICE 
###############################################################################

# Get the device the user wants to install onto.
INSTALLDEV="novalue"
until [[ ${INSTALLDEV} != "" && -b ${INSTALLDEV} ]]
do
	echo
	echo "The following devices are available for installation:"
	until [[ ! -z "${FOO}" ]]
	do
		FOO=`$DIRNAME/../setup/pd_setup_files/iofindwholemedia`
	done
	$DIRNAME/../setup/pd_setup_files/iofindwholemedia
	printf "Which device would you like to install PureDarwin onto? "
	read INSTALLDEV

	if [ "${INSTALLDEV}" == "s" ]; then
		# Drop into a shell (resume at exit)
		/bin/sh
	else
		# Check and retrieve the disk device node (e.g., /dev/disk1)
		T_INSTALLDEV="$INSTALLDEV"
		INSTALLDEV=`$DIRNAME/../setup/pd_setup_files/iofindwholemedia ${INSTALLDEV}`

		if [ ! -b "${INSTALLDEV}" ]; then
			echo
			echo "\"${T_INSTALLDEV}\" doesn't exist, please enter one of the listed devices."
		fi
	fi
done

###############################################################################
# SETUP BASE
###############################################################################

PUREDARWIN_RELEASE=""
until [ ! : ]
do
	echo
	echo "The following releases are available for installation: "
	echo "1. PureDarwin"
	echo "2. Puredarwin boostrap"
	echo "3. PureDarwin nano"
	printf "Your Choice: "
	read CHOICE
	case ${CHOICE} in
		s) /bin/sh
		   ;;   
		1) PUREDARWIN_RELEASE="" /usr/local/share/puredarwin/setup/pd_setup "$INSTALLDEV" PureDarwin
		   break
		   ;;
		2) PUREDARWIN_RELEASE="bootstrap"
		   PUREDARWIN_RELEASE="bootstrap" /usr/local/share/puredarwin/setup/pd_setup "$INSTALLDEV" PureDarwin
		   break
		   ;;
		3) PUREDARWIN_RELEASE="nano"
		   PUREDARWIN_RELEASE="nano" /usr/local/share/puredarwin/setup/pd_setup "$INSTALLDEV" PureDarwin
		   break
		   ;;
		*) echo
		   echo "\"${CHOICE}\" doesn't exist, please enter one of the listed releases."
		   ;;
	esac
done

echo
echo "Installation of the base system should now be complete."

###############################################################################
# POST CONFIG 
###############################################################################

# FIXME: needed? where?
# Unpopulating /dev 
#/sbin/umount /dev

until [ ! : ]
do
	echo
	echo "You may: "
	echo "1. Reboot" 
	echo "2. Spawn a shell"
	if [ "$PUREDARWIN_RELEASE" = "" ]; then
		echo "3. Add a user"
	fi	
	printf "Your Choice: "
	read CHOICE
	echo
	case ${CHOICE} in
		s) /bin/sh
		   ;;   
		1) break
		   ;;
		2) /bin/sh
		   ;;
		3) if [ "$PUREDARWIN_RELEASE" = "" ]; then
			add_user "$INSTALLDEV" PureDarwin
		   fi	
		   ;;
		*) echo "\"${CHOICE}\" doesn't exist, please enter one of the listed choice."
		   ;;
	esac
done


###############################################################################
# REBOOT 
###############################################################################

echo
echo "Installation should now be complete. Press 'enter' to restart"
read RESTART
echo ""
reboot

# bash interactive
#/bin/bash -i 

# zsh interactive
#/bin/zsh -i

